package com.hoshiibuy.platform.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.platform.entity.PlatformDepartments;
import com.hoshiibuy.platform.entity.param.PlatformDepartmentsInsertParam;
import com.hoshiibuy.platform.entity.param.PlatformDepartmentsParam;
import com.hoshiibuy.platform.entity.param.PlatformDepartmentsUpdateParam;
import com.hoshiibuy.platform.service.PlatformDepartmentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-22
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/platformDepartments")
@Api(value = "平台部门管理", tags = {"平台部门操作接口"})
public class PlatformDepartmentsController {

    @Autowired
    private PlatformDepartmentsService platformDepartmentsService;

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 分页查询平台部门信息
     */
    @ApiOperation(value = "分页查询平台部门记录")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PlatformDepartmentsParam param) {
        try {
            log.info("分页查询平台信息");

            QueryWrapper<PlatformDepartments> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(param.getName()))
                queryWrapper.lambda().eq(PlatformDepartments::getName, param.getName());

            if (param.getPlatformId() != 0)
                queryWrapper.lambda().eq(PlatformDepartments::getPlatformId, param.getPlatformId());


            IPage<PlatformDepartments> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

            return Response.data(platformDepartmentsService.page(page, queryWrapper));

        } catch (ServiceException e) {
            log.error("查询平台部门信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("分页查询平台部门信息异常", e);
            return Response.failed("分页查询平台部门信息异常");

        }

    }

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询平台部门信息
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询平台部门信息")
    public Response queryPlatformDepartmentsById(Long id) {
        log.info("查询平台部门信息");
        return Response.data(platformDepartmentsService.getById(id));
    }

    /**
     * @param name
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/query/param")
    @ApiOperation(value = "查询平台部门信息")
    public Response queryPlatformDepartmentsByName(String name) {

        PlatformDepartments platformDepartments;
        try {

            Wrapper wrapper = Wrappers.<PlatformDepartments>query()
                    .lambda().eq(PlatformDepartments::getName, name);

            platformDepartments = platformDepartmentsService.getOne(wrapper);

        } catch (ServiceException e) {

            log.error("查询平台部门信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("查询平台部门信息异常", e);
            return Response.failed("查询平台部门信息异常");
        }


        return Response.data(platformDepartments);

    }

    /**
     * @param platformDepartments
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台部门信息")
    public Response savePlatformDepartments(@RequestBody @Valid PlatformDepartmentsInsertParam platformDepartmentsInsertParam) {
        try {
            PlatformDepartments platformDepartments = BeanUtils.covertType(platformDepartmentsInsertParam, PlatformDepartments.class);
            return Response.data(platformDepartmentsService.save(platformDepartments));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }
    }

    /**
     * @param platformDepartments
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台部门信息")
    public Response updatePlatformDepartmentsById(@RequestBody @Valid PlatformDepartmentsUpdateParam platformDepartmentsUpdateParam) {
        try {
            PlatformDepartments platformDepartments = BeanUtils.covertType(platformDepartmentsUpdateParam, PlatformDepartments.class);
            return Response.data(platformDepartmentsService.updateById(platformDepartments));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }


    }
}

